package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.PictureAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.bean.MyPictureBean;
import flc.ast.databinding.ActivityPriAlbumBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;

public class PriAlbumActivity extends BaseAc<ActivityPriAlbumBinding> {

    private PictureAdapter pictureAdapter;
    private List<MyPictureBean> listShow = new ArrayList<>();
    private Dialog myMoreDialog;
    private Dialog myRenameDialog;
    private EditText dialogRename;
    private boolean isEdit = false;
    private List<String> listSel = new ArrayList<>();
    private Dialog myTipDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_pri_album;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivPriAlbumBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.ivPrivacyAlbumAdd.setOnClickListener(this);
        mDataBinding.tvAlbumCancel.setOnClickListener(this);
        mDataBinding.ivAlbumMore.setOnClickListener(this);
        mDataBinding.rlAlbumClearView.setOnClickListener(this);
        mDataBinding.rvAlbumList.setLayoutManager(new GridLayoutManager(mContext, 3));
        pictureAdapter = new PictureAdapter();
        mDataBinding.rvAlbumList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
        //更多弹框
        moreDialog();
        //提示弹框
        tipDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPrivacyAlbumAdd://添加相册
                SelPicActivity.isMore = true;
                SelPicActivity.type = 9;
                Intent intent1 = new Intent(this, SelPicActivity.class);
                startActivityForResult(intent1, Content.REQUEST_ADD);
                break;
            case R.id.ivAlbumMore://更多
                myMoreDialog.show();
                break;
            case R.id.tvAlbumCancel://取消编辑
                cancelEdit();
                break;
            //更多弹框
            case R.id.tvDialogMoreAdd://添加
                myMoreDialog.dismiss();
                SelPicActivity.isMore = true;
                SelPicActivity.type = 9;
                Intent intent = new Intent(this, SelPicActivity.class);
                startActivityForResult(intent, Content.REQUEST_ADD);
                break;
            case R.id.tvDialogMoreRename://重命名
                myMoreDialog.dismiss();
                dialogRename.setText("");
                myRenameDialog.show();
                break;
            case R.id.tvDialogMoreAllSel://多选
                myMoreDialog.dismiss();
                isEdit = true;
                mDataBinding.ivAlbumMore.setVisibility(View.GONE);
                mDataBinding.tvAlbumCancel.setVisibility(View.VISIBLE);
                mDataBinding.rlAlbumClearView.setVisibility(View.VISIBLE);
                pictureAdapter.setEdit(true);
                pictureAdapter.notifyDataSetChanged();
                break;
            case R.id.rlAlbumClearView://多选删除
                if (listSel.size() == 0) return;
                myTipDialog.show();
                break;
            case R.id.tvDialogMoreClear://清空
                myMoreDialog.dismiss();
                listSel.clear();
                for (MyPictureBean bean : pictureAdapter.getValidData()) {
                    listSel.add(bean.getPath());
                }
                deleteAlbum();
                break;
            case R.id.tvDialogMoreCancel://取消
                myMoreDialog.dismiss();
                break;
            //提示弹框
            case R.id.tvDialogTipCancel://取消
                myTipDialog.dismiss();
                break;
            case R.id.tvDialogTipRight://确定
                myTipDialog.dismiss();
                deleteAlbum();
                cancelEdit();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            //多选
            if (pictureAdapter.getItem(position).isSelect()) {
                pictureAdapter.getItem(position).setSelect(false);
                listSel.remove(pictureAdapter.getItem(position).getPath());
            } else {
                pictureAdapter.getItem(position).setSelect(true);
                listSel.add(pictureAdapter.getItem(position).getPath());
            }
            pictureAdapter.notifyItemChanged(position);
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    //获取数据
    private void getData() {
        listShow.clear();
        List<String> listSp = AppSpUtils.getPrivacyAlbumList();
        if (listSp != null && listSp.size() > 0) {
            for (String path : listSp) {
                listShow.add(new MyPictureBean(path, false));
            }
            pictureAdapter.setList(listShow);
            mDataBinding.rvAlbumList.setVisibility(View.VISIBLE);
            mDataBinding.llPrivacyAlbumNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvAlbumList.setVisibility(View.GONE);
            mDataBinding.llPrivacyAlbumNoData.setVisibility(View.VISIBLE);
        }
    }

    //取消编辑
    private void cancelEdit() {
        listSel.clear();
        isEdit = false;
        mDataBinding.ivAlbumMore.setVisibility(View.VISIBLE);
        mDataBinding.tvAlbumCancel.setVisibility(View.GONE);
        mDataBinding.rlAlbumClearView.setVisibility(View.GONE);
        pictureAdapter.setEdit(false);
        for (MyPictureBean bean : pictureAdapter.getValidData()) {
            bean.setSelect(false);
        }
        pictureAdapter.notifyDataSetChanged();
    }

    //删除相册图片
    private void deleteAlbum() {
        List<String> listSp = AppSpUtils.getPrivacyAlbumList();
        listSp.removeAll(listSel);
        AppSpUtils.savePrivacyAlbumList(listSp);
        getData();
    }

    //更多弹框
    private void moreDialog() {
        myMoreDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_more, null);
        myMoreDialog.setContentView(inflate);
        myMoreDialog.setCancelable(true);
        //获取当前Activity所在的窗体
        Window dialogWindow = myMoreDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.95);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView dialogAdd = inflate.findViewById(R.id.tvDialogMoreAdd);
        TextView dialogRename = inflate.findViewById(R.id.tvDialogMoreRename);
        TextView dialogAllSel = inflate.findViewById(R.id.tvDialogMoreAllSel);
        TextView dialogClear = inflate.findViewById(R.id.tvDialogMoreClear);
        TextView dialogCancel = inflate.findViewById(R.id.tvDialogMoreCancel);
        dialogAdd.setOnClickListener(this);
        dialogRename.setOnClickListener(this);
        dialogAllSel.setOnClickListener(this);
        dialogClear.setOnClickListener(this);
        dialogCancel.setOnClickListener(this);
        dialogRename.setVisibility(View.GONE);
    }

    //提示弹框
    private void tipDialog() {
        myTipDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_tip, null);
        myTipDialog.setContentView(inflate);
        myTipDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myTipDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.95);
        lp.y = 20;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView dialogClose = inflate.findViewById(R.id.tvDialogTipCancel);
        TextView dialogCom = inflate.findViewById(R.id.tvDialogTipRight);
        dialogClose.setOnClickListener(this);
        dialogCom.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == Content.REQUEST_ADD) {
            List<String> listPath = (List<String>) data.getSerializableExtra(Content.LIST_PATH);
            if (listPath != null && listPath.size() > 0) {
                List<String> listSp = AppSpUtils.getPrivacyAlbumList();
                if (listSp != null && listSp.size() > 0) {
                    listSp.addAll(listPath);
                    AppSpUtils.savePrivacyAlbumList(listSp);
                } else {
                    AppSpUtils.savePrivacyAlbumList(listPath);
                }
                getData();
            }
        }
    }

}